package com.imooc.service.impl;

import com.imooc.entity.bo.UpdateUserBo;
import com.imooc.entity.pojo.Users;
import com.imooc.enums.Sex;
import com.imooc.enums.UserInfoModifyType;
import com.imooc.enums.YesOrNo;
import com.imooc.exceptions.GraceException;
import com.imooc.grace.result.GraceJSONResult;
import com.imooc.grace.result.ResponseStatusEnum;
import com.imooc.mapper.UsersMapper;
import com.imooc.service.UsersService;
import com.imooc.utils.DateUtil;
import com.imooc.utils.DesensitizationUtil;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service

public class UsersServiceImpl implements UsersService {
    @Autowired
    private UsersMapper userMapper;
    @Autowired
    private Sid sid;
    public static final String USER_FACE = "http://39.105.159.64:9000/hero-nintendo/2025/02/6f8ff55d-a967-43b0-a1c8-ea5826ae89a2.png";

    /**
     * 根据手机号查询用户是否存在
     *
     * @param mobile
     * @return
     */
    @Override
    public Users findUserIsExist(String mobile) {
        return userMapper.findUserIsExist(mobile);
    }

    /**
     * 用户不存在注册用户 并返回用户信息
     *
     * @param mobile
     * @return
     */
    @Override
    public Users queryOrSaveUserInfo(String mobile) {
        Users user = new Users();
        user.setId(sid.nextShort());
        user.setMobile(mobile);
        user.setNickname("用户:" + DesensitizationUtil.commonDisplay(mobile));
        user.setImoocNum("用户:" + DesensitizationUtil.commonDisplay(mobile));
        user.setFace(USER_FACE);
        user.setBirthday(DateUtil.stringToDate("1900-01-01"));
        user.setSex(Sex.secret.type);
        user.setCountry("中国");
        user.setProvince("");
        user.setCity("");
        user.setDistrict("");
        user.setDescription("这个家伙很赖什么也没因留下~~~");
        user.setCanImoocNumBeUpdated(YesOrNo.YES.type);
        user.setCreatedTime(new Date());
        user.setUpdatedTime(new Date());
        userMapper.saveUser(user);
        return user;
    }

    @Override
    public Users findUserByUserId(String userId) {
        return userMapper.findUserByUserId(userId);
    }
    @Transactional
    @Override
    public Users updateUserInfo(UpdateUserBo updateUserBo, Integer type) {
        if (type == UserInfoModifyType.NICKNAME.type) {
            Users user = userMapper.findUserByNickname(updateUserBo.getNickname());
            if (user != null) {
                GraceException.display(ResponseStatusEnum.USER_INFO_UPDATED_NICKNAME_EXIST_ERROR);
            }
                userMapper.updateUserInfo(updateUserBo);
        }
        if (type == UserInfoModifyType.IMOOCNUM.type) {
            Users user = userMapper.findUserByUserId(updateUserBo.getId());
            userMapper.updateUserInfo(updateUserBo);
            userMapper.updateUserCanImoocNumBeUpdated(user.getId());
        }
        if(type==UserInfoModifyType.SEX.type){
            userMapper.updateUserInfo(updateUserBo);
        }
        if(type==UserInfoModifyType.BIRTHDAY.type){
            userMapper.updateUserInfo(updateUserBo);
        }
        if(type==UserInfoModifyType.LOCATION.type){
            userMapper.updateUserInfo(updateUserBo);
        }
        if(type==UserInfoModifyType.DESC.type){
            userMapper.updateUserInfo(updateUserBo);
        }
        return userMapper.findUserByUserId(updateUserBo.getId());
    }

    @Override
    public void updateUserInfo(UpdateUserBo updateUserBo) {
        userMapper.updateUserInfo(updateUserBo);
    }
}
